package dfs_bfs.test257;

import listbyorder.utils.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/8 14:23
 */
public class Solution1 {

    // DFS
    List<String> ans = new ArrayList<>();

    public List<String> binaryTreePaths(TreeNode root) {
        if (root == null) return ans;
        dfs(root, new StringBuilder());
        return ans;
    }

    private void dfs(TreeNode root, StringBuilder path) {
        if (root == null) return;
        if (path.length() != 0) path.append("->");
        path.append(root.val + "");
        StringBuilder left = new StringBuilder(path);
        if (root.left == null && root.right == null) {
            ans.add(new String(path));
        } else {
            dfs(root.left, left);
            dfs(root.right, path);
        }
    }

}
